﻿using System;
using System.Configuration;
using System.Data;
using System.Web.UI.WebControls;
using System.Data.SqlClient;

namespace Web.forms
{
    public partial class Modulos : Pagina
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack) return;

            Cargar_GridView();
            CargaPerfiles();
            Cargar_Modulos();
           
            Txt_Modulo.Text = BLL.Modulos.ObtenerMaxId().ToString();
        }

        protected void CargaPerfiles()
        {
            Util.Common.CargaDropDownList(ref ddlPerfiles, BLL.Modulos.ObtenerPerfiles(), "Glosa", "PerfilId", "SELECCIONE PERFIL");
        }

        private void Cargar_Modulos()
        {
            Util.Common.CargaDropDownList(ref ddlModuloPadre, BLL.Modulos.ObtenerModulos(), "Glosa", "ModuloId", "SELECCIONE MODULO");
            ddlModuloPadre.Items.Add(new ListItem("RAIZ DE APLICACION (/)", "1"));
        }

        private void Cargar_GridView()
        {
            GridView_Modulos.DataSource = BLL.Modulos.ObtenerModulos();
            GridView_Modulos.DataBind();
        }

        private void LimpiaForm()
        {
            Txt_Modulo.Text = BLL.Modulos.ObtenerMaxId().ToString();
            Txt_Modulo.Enabled = false;
            Txt_Glosa.Text = "";
            Txt_URL_Modulo.Text = "";

            ddlModuloPadre.SelectedIndex = -1;

            txtPosicion.Text = "";

            Cargar_GridView();
            Cargar_Modulos();
        }

        protected void GridView_Modulos_SelectedIndexChanged(object sender, EventArgs e)
        {

            Txt_Modulo.Text = ((Label)GridView_Modulos.SelectedRow.FindControl("Label8")).Text;
            Txt_Glosa.Text = ((Label)GridView_Modulos.SelectedRow.FindControl("Label9")).Text;
            Txt_URL_Modulo.Text = ((Label)GridView_Modulos.SelectedRow.FindControl("Label10")).Text;
            ddlModuloPadre.Text = ((Label)GridView_Modulos.SelectedRow.FindControl("Label11")).Text;
            txtPosicion.Text = ((Label)GridView_Modulos.SelectedRow.FindControl("Label12")).Text;

            if (ddlPerfiles.Items.FindByText(((Label)GridView_Modulos.SelectedRow.FindControl("Label13")).Text) != null)
            {
                ddlPerfiles.SelectedIndex = -1;
                ddlPerfiles.Items.FindByText(((Label)GridView_Modulos.SelectedRow.FindControl("Label13")).Text).Selected = true;
            }

            Cargar_GridView();
        }

        protected void Btn_Aprobar_Click(object sender, EventArgs e)
        {

            string v_OK;

            v_OK = "OK";

            if (Txt_Modulo.Text == "") { v_OK = "NOK"; }
            if (Txt_Glosa.Text == "") { v_OK = "NOK"; }
            if (Txt_URL_Modulo.Text == "") { v_OK = "NOK"; }
            if (ddlModuloPadre.Text == "") { v_OK = "NOK"; }
            if (txtPosicion.Text == "") { v_OK = "NOK"; }

            if (v_OK == "OK")
            {

                string connstring = ConfigurationManager.ConnectionStrings["connDB"].ToString();
                SqlConnection conn = new SqlConnection(connstring);
                SqlCommand cmd = new SqlCommand();

                cmd.CommandText = "SP_INS_MODULOS";
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Connection = conn;

                SqlParameter sqlparam1 = new SqlParameter("@MODULO_ID", SqlDbType.Int, 3);
                SqlParameter sqlparam2 = new SqlParameter("@GLOSA", SqlDbType.VarChar, 20);
                SqlParameter sqlparam3 = new SqlParameter("@URL", SqlDbType.VarChar, 100);
                SqlParameter sqlparam4 = new SqlParameter("@MODULO_PADRE", SqlDbType.Int,4);
                SqlParameter sqlparam5 = new SqlParameter("@POSICION", SqlDbType.Int, 4);
                SqlParameter sqlparamPer = new SqlParameter("@PERFIL", SqlDbType.Int, 1);
                SqlParameter sqlparam6 = new SqlParameter("@RETORNO", SqlDbType.Int, 1);
                SqlParameter sqlparam7 = new SqlParameter("@MENSAJE", SqlDbType.VarChar, 100);

                sqlparam1.Value = Txt_Modulo.Text;
                sqlparam2.Value = Txt_Glosa.Text;
                sqlparam3.Value = Txt_URL_Modulo.Text;
                sqlparam4.Value = ddlModuloPadre.Text;
                sqlparam5.Value = txtPosicion.Text;
                sqlparam6.Value = 0;
                sqlparam7.Value = "";
                sqlparamPer.Value = ddlPerfiles.SelectedValue;

                cmd.Parameters.Add(sqlparam1);
                cmd.Parameters.Add(sqlparam2);
                cmd.Parameters.Add(sqlparam3);
                cmd.Parameters.Add(sqlparam4);
                cmd.Parameters.Add(sqlparam5);
                cmd.Parameters.Add(sqlparamPer);
                cmd.Parameters.Add(sqlparam6);
                cmd.Parameters.Add(sqlparam7);

                conn.Open();
                cmd.ExecuteNonQuery();
                conn.Close();
                // Cargar_GridView();
                LimpiaForm();
            }
            else
                Response.Write("<script type=\"text/javascript\">alert('Para Grabar Favor LLenar todos los campos Requeridos..');</script>");
        }
        protected void GridView_Modulos_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Eliminar")
            {

                int index = Convert.ToInt32(e.CommandArgument);
                string connstring = ConfigurationManager.ConnectionStrings["connDB"].ToString();
                SqlConnection conn = new SqlConnection(connstring);
                SqlCommand cmd = new SqlCommand();

                cmd.CommandText = "sp_del_Modulos";
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Connection = conn;

                SqlParameter sqlparam1 = new SqlParameter("@MODULO_ID", SqlDbType.Int, 3);
                SqlParameter sqlparam2 = new SqlParameter("@RETORNO", SqlDbType.Int, 1);
                SqlParameter sqlparam3 = new SqlParameter("@MENSAJE", SqlDbType.VarChar, 100);

                sqlparam1.Value = index;
                sqlparam2.Value = 0;
                sqlparam3.Value = "";

                cmd.Parameters.Add(sqlparam1);
                cmd.Parameters.Add(sqlparam2);
                cmd.Parameters.Add(sqlparam3);

                conn.Open();
                cmd.ExecuteNonQuery();
                conn.Close();

                LimpiaForm();

            }
        }

        protected void btnNuevo_Click(object sender, EventArgs e)
        {
            LimpiaForm();
        }

       

    

    }
}
